/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cdp.cdf.component.metadata.obsolete.serializer;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.inspur.edp.caf.cef.schema.base.utils.JsonUtil;
import com.inspur.edp.cdf.component.api.exception.CommonComponentException;
import com.inspur.edp.cdp.cdf.component.metadata.exception.MetadataExceptionCode;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.Classification;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.ComponentParameter;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.BasicType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.ComplexType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.DictionaryType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.ListType;
import com.inspur.edp.cdp.cdf.component.metadata.obsolete.metadata.datatype.UnsupportType;

import java.io.IOException;
import java.util.Objects;

/**
 * 功能描述:
 *
 * @ClassName: ObseleteComponentParameterDeSerialzier
 * @Author: Fynn Qi
 * @Date: 2020/2/19 11:37
 * @Version: V1.0
 */
public class ObseleteComponentParameterDeSerialzier extends JsonDeserializer<ComponentParameter> {

    @Override
    public ComponentParameter deserialize(JsonParser p, DeserializationContext ctxt)
        throws IOException, JsonProcessingException {

        if (Objects.isNull(p)) {
            return null;
        }
        ComponentParameter parameter = new ComponentParameter();
        JsonNode node = p.getCodec().readTree(p);
        parameter.setId(JsonUtil.getPropValue(node, "Id", String.class));
        parameter.setCode(JsonUtil.getPropValue(node, "Code", String.class));
        parameter.setName(JsonUtil.getPropValue(node, "Name", String.class));
        parameter.setDescription(JsonUtil.getPropValue(node, "Description", String.class));
        parameter.setHasReturnValue(JsonUtil.getPropValue(node, "IsReturn", boolean.class));
        parameter.setRequired(JsonUtil.getPropValue(node, "IsRequired", boolean.class));
        parameter.setClassification(JsonUtil.getPropValue(node, "Classification", Classification.class));
        JsonNode typeNode = node.get("DataType");
        if (parameter.getClassification() == Classification.BASIC) {
            getBasicType(typeNode, parameter);
        } else if (parameter.getClassification() == Classification.COMPLEX) {
            getComplexType(typeNode, parameter);
        } else if (parameter.getClassification() == Classification.DICTIONARY) {
            getDictionaryType(typeNode, parameter);
        } else if (parameter.getClassification() == Classification.LIST) {
            getListType(typeNode, parameter);
        } else if (parameter.getClassification() == Classification.UNSUPPORT) {
            getUnSupportType(typeNode, parameter);
        } else {
            throw new CommonComponentException(MetadataExceptionCode.SERVICE_UNIT_CODE, MetadataExceptionCode.RESOURCE_FILE,
                MetadataExceptionCode.GSP_COMMONCMP_METADATA_0101, null, false, parameter.getClassification().toString());
        }
        return parameter;
    }

    private void getUnSupportType(JsonNode typeNode, ComponentParameter parameter) {
        UnsupportType dataType = new UnsupportType();
        dataType.setParamType(typeNode.get("ParamType") == null ? null : typeNode.get("ParamType").toString());
        parameter.setDataType(dataType);
    }

    private void getListType(JsonNode typeNode, ComponentParameter parameter) {
        ListType dataType = new ListType();
        dataType.setBasic(typeNode.get("IsBasic") == null ? null : typeNode.get("IsBasic").asBoolean());
        dataType.setModelId(typeNode.get("ModelId") == null ? null : typeNode.get("ModelId").toString());
        dataType.setModelName(typeNode.get("ModelName") == null ? null : typeNode.get("ModelName").toString());

        parameter.setDataType(dataType);
    }

    private void getDictionaryType(JsonNode typeNode, ComponentParameter parameter) {
        DictionaryType dataType = new DictionaryType();
        dataType.setBasicKey(typeNode.get("IsTkeyBasic") == null ? null : typeNode.get("IsTkeyBasic").asBoolean());
        dataType.setKey(typeNode.get("Tkey") == null ? null : typeNode.get("Tkey").toString());
        dataType.setKeyModelId(typeNode.get("TkeyModelId") == null ? null : typeNode.get("TkeyModelId").toString());

        dataType.setBasicValue(typeNode.get("IsTValueBasic") == null ? null : typeNode.get("IsTValueBasic").asBoolean());
        dataType.setValue(typeNode.get("TValue") == null ? null : typeNode.get("TValue").toString());
        dataType.setValueModelId(typeNode.get("TValueModelId") == null ? null : typeNode.get("TValueModelId").toString());

        parameter.setDataType(dataType);
    }

    private void getComplexType(JsonNode typeNode, ComponentParameter parameter) {
        ComplexType dataType = new ComplexType();
        dataType.setModelId(typeNode.get("ModelId") == null ? null : typeNode.get("ModelId").toString());
        dataType.setModelName(typeNode.get("ModelName") == null ? null : typeNode.get("ModelName").toString());
        parameter.setDataType(dataType);
    }

    private void getBasicType(JsonNode typeNode, ComponentParameter parameter) {
        BasicType dataType = new BasicType();
        dataType.setParamType(typeNode.get("ParamType") == null ? null : typeNode.get("ParamType").toString());
        parameter.setDataType(dataType);
    }
}
